using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace KNCApp.GUI
{
    public partial class ArchiveDataForm : Form
    {
        #region MEMBERS
        List<Business.Models.Result> Results = null;
        List<Business.Models.Result> FilteredResults = null;
        #endregion
        public ArchiveDataForm()
        {
            InitializeComponent();
        }

        public ArchiveDataForm(Business.Models.TestPilot tp)
        {
            InitializeComponent();

            this.tbFname.Text = tp.Pilot.FirstName;
            this.tbLname.Text = tp.Pilot.LastName;
            this.dtpDOB.Value = tp.Pilot.DoB;

            this.tbDescription.Text = tp.Test.Name;
            this.dtTestDate.Value = tp.Test.Date;

            Results = 
                KNCApp.DataSource.DataSourceProvider.GetDS().SelectResults(tp);

            FilteredResults = Results;

            if(Results.Count == 0)//no results
                return;

            this.DrawCharts(Results);
            resultBindingSource.DataSource = Results;

            DateTime min = Results[0].Time;
            DateTime max = Results[Results.Count-1].Time;

            this.dtFrom.Value = min;
            this.dtTo.Value = max;

            FilterByDate(min, max);


        }

        #region Events
        private void btnFilter_Click(object sender, EventArgs e)
        {
            FilteredResults = new List<Business.Models.Result>();

            int i = 0;
            foreach(Business.Models.Result res in Results)
            {
                if(i%this.numericUpDown.Value == 0)
                    FilteredResults.Add(res);
                i++;
            }
            this.DrawCharts(FilteredResults);
            resultBindingSource.DataSource = FilteredResults;

            FilterByDate(this.dtFrom.Value, this.dtTo.Value);
        }



        private void OnFormClosed(object sender, FormClosedEventArgs e)
        {
            KNCApp.DataSource.DataSourceProvider.GetDS().RestoreDefConnection();
        }
        #endregion

        #region PRIVATE
        private void DrawCharts(List<Business.Models.Result> res)
        {
            chartBreath.Series.Values.Clear();
            chartPulse.Series.Values.Clear();
            chartSPO2.Series.Values.Clear();
            chartETCO2.Series.Values.Clear();
            chartPressure.Series.Values.Clear();

            foreach(Business.Models.Result r in res)
            {
                chartBreath.Series.Values.Add(
                    new CristiPotlog.ChartControl.ChartSeriesValue(r.Time, r.Breath));

                chartPulse.Series.Values.Add(
                    new CristiPotlog.ChartControl.ChartSeriesValue(r.Time, r.Pulse));


                chartSPO2.Series.Values.Add(
                    new CristiPotlog.ChartControl.ChartSeriesValue(r.Time, r.SPO2));


                chartETCO2.Series.Values.Add(
                    new CristiPotlog.ChartControl.ChartSeriesValue(r.Time, r.ETCO2));


                if(r.Pressure.Value != Decimal.MinValue)
                {
                    chartPressure.Series.Values.Add(
                        new CristiPotlog.ChartControl.ChartSeriesValue(r.Time, r.Pressure.Value));
                }
            }

            chartBreath.Invalidate();
            chartPulse.Invalidate();
            chartSPO2.Invalidate();
            chartETCO2.Invalidate();
            chartPressure.Invalidate();

        }
        
        private void FilterByDate(DateTime min, DateTime max)
        {
            chartBreath.XAxis.Scale.Minimum = min;
            chartBreath.XAxis.Scale.Maximum = max;

            chartPulse.XAxis.Scale.Minimum = min;
            chartPulse.XAxis.Scale.Maximum = max;

            chartSPO2.XAxis.Scale.Minimum = min;
            chartSPO2.XAxis.Scale.Maximum = max;

            chartETCO2.XAxis.Scale.Minimum = min;
            chartETCO2.XAxis.Scale.Maximum = max;

            chartPressure.XAxis.Scale.Minimum = min;
            chartPressure.XAxis.Scale.Maximum = max;
        }

        #endregion

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            ArchiveDataReport r = new ArchiveDataReport(this.FilteredResults);
            r.ShowDialog();
        }

        private void ArchiveDataForm_Load(object sender, EventArgs e)
        {

        }

    }
}